<?php


namespace App\Controller\Web;


use App\Base\BaseController4Restful;
use App\Base\BaseController4Web;
use App\Repository\ConfigRepository;
use App\Repository\PageRepository;
use App\Service\Wechat\WechatPay;
use App\Service\WechatService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

//域名：www.zhangshangjunshi.com


class JunshiController extends BaseController4Web
{


    /**
     * @Route("/wap/page", name="webSinglePage")
     */
    public function webPage(PageRepository $pageRepository)
    {
        $data["root"] = "/web";
        $data["rnd"] = "?t=".time();

        if(!$code = $this->getParamRequired("code")){return $this->json_out(0,"");}
        $page = $pageRepository->findOneBy(array("code"=>$code));
        if (!$page) {
            die();
        }
        $data["title"] = $page->getTitle();
        $data["content"] = $page->getContent();
        return $this->render("/Wap/page.html.twig",$data);
    }

    /**
     * @Route("/wap/login", name="WapLogin")
     */
    public function WapLogin()
    {
        $data["root"] = "/web";
        $data["rnd"] = "?t=".time();

        return $this->render("/Wap/login.html.twig",$data);
    }

    /**
     * @Route("/wap/reg", name="WapReg")
     */
    public function WapReg()
    {
        $data["root"] = "/web";
        $data["rnd"] = "?t=".time();
        if(!$phone = $this->getParamRequired("phone")){$phone = "";}
        $data["phone"] = $phone;

        return $this->render("/Wap/reg.html.twig",$data);
    }

    /**
     * @Route("/wap/pay", name="WapPay")
     */
    public function WapPay(WechatService $wechatService,ConfigRepository $configRepository,Request $request)
    {
//        dd($request->getHttpHost());
        if(!$code = $this->getParamRequired("code")){
            $wechatService->getWeChatCode("http://".$request->getHttpHost()."/wap/pay");
        }

        $wechatService->getOpenidAndAccessToken($code);
        $openid = $wechatService->Openid;
        $this->mySession()->set("openid",$openid);
//        dd($openid);

        $data["root"] = "/web";
        $data["rnd"] = "?t=".time();
        $data["jsapi_param"] = $this->getP($wechatService);

        $money = $configRepository->getByK("MONEY_SET");
        $money = $this->myExplodeNumbers($money);
        $moneySet = [];
        $cound = floor(count($money)/2);
//        dd($cound);
        for ($i= 0;$i<$cound;$i++) {
            $moneySet[]=[$money[$i * 2],$money[$i * 2 + 1]];
        }
        $data["moneySet"]=$moneySet;

        return $this->render("/Wap/pay.html.twig",$data);
    }

    function getP (WechatService $wechatService)
    {
        $request = Request::createFromGlobals();
//        $WechatModel = new WechatModel($this->doctine);
        $wechatService->getAccessToken();
        $wechatService->getJsApiTicket();
        //微信分享签名
        $now = time();
        $JsApiParam["appId"] = $wechatService->AppId;
        $JsApiParam["timestamp"] = $now;
        $JsApiParam["noncestr"] = sha1($now);
        $JsApiParam["url"] = $request->getUri();

        $jsapi_ticket = $wechatService->JsApiTicket;
        $timestamp = $JsApiParam["timestamp"];
        $noncestr = $JsApiParam["noncestr"];
        $url = $JsApiParam["url"];
        $signature =  sha1("jsapi_ticket=$jsapi_ticket&noncestr=$noncestr&timestamp=$timestamp&url=$url");

        $JsApiParam["signature"] = $signature;
        $JsApiParam["url"] = $request->getUri();
        $JsApiParam["ticket"] = $wechatService->JsApiTicket;
        $JsApiParam["str1"] = "jsapi_ticket=$jsapi_ticket&noncestr=$noncestr&timestamp=$timestamp&url=$url";
        return $JsApiParam;
    }



    /**
     * @Route("/wap/dl", name="WapDownLoad")
     */
    public function WapDownLoad()
    {
        $data["root"] = "/web";
        $data["rnd"] = "?t=".time();

        return $this->render("/Wap/download.html.twig",$data);
    }


    /**
     * @Route("/", name="WebIndexPage")
     */
    public function WebIndexPage()
    {
        $data["root"] = "/web";
        $data["rnd"] = "?t=".time();

        return $this->render("/Wap/index1.html.twig",$data);
    }


}